package com.eastfair.tag.controller;

import cn.hutool.core.bean.BeanUtil;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.tag.dto.TagTargetBusinessDTO;
import com.eastfair.tag.entity.TagTargetForExhibitor;
import com.eastfair.tag.entity.TagTargetForExhibitorProduct;
import com.eastfair.tag.entity.TagTargetForVisitor;
import com.eastfair.tag.mongoDao.TagTargetForExhibitorMongoDao;
import com.eastfair.tag.mongoDao.TagTargetForExhibitorProductMongoDao;
import com.eastfair.tag.mongoDao.TagTargetForVisitorMongoDao;
import com.eastfair.tag.service.BaseTagTargetService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * 前端控制器
 *
 * </p>
 *
 * @author zjn
 * @date 2022-03-11
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/tagTargetBusiness")
public class TagTargetBusinessController {
    @Autowired
    private BaseTagTargetService baseTagTargetService;
    @Autowired
    private TagTargetForExhibitorProductMongoDao exhibitorProductMongoDao;
    @Autowired
    private TagTargetForExhibitorMongoDao exhibitorMongoDao;
    @Autowired
    private TagTargetForVisitorMongoDao visitorMongoDao;

    /**
     * 主体标签直接绑定
     *
     * @param tagTargetBusinessDTO 主体标签信息
     * @return 是否成功
     */
    @ApiOperation("主体标签直接绑定")
    @PostMapping({"/saveTagTarget"})
    public R<Boolean> saveTagTarget(@RequestBody TagTargetBusinessDTO tagTargetBusinessDTO) {
        if (TagTargetForExhibitor.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.saveTagTarget(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForExhibitor.class), exhibitorMongoDao);
        } else if (TagTargetForExhibitorProduct.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.saveTagTarget(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForExhibitorProduct.class), exhibitorProductMongoDao);
        } else if (TagTargetForVisitor.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.saveTagTarget(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForVisitor.class), visitorMongoDao);
        } else {
            return R.fail(ExceptionCode.NOT_FOUND);
        }
        return R.successDef();
    }

    /**
     * 主体标签间接绑定
     *
     * @param tagTargetBusinessDTO 主体资源信息
     * @return 是否成功
     */
    @ApiOperation("主体标签间接绑定")
    @PostMapping({"/saveTagTargetByResourceType"})
    public R<Boolean> saveTagTargetByResourceType(@RequestBody TagTargetBusinessDTO tagTargetBusinessDTO) {
        if (TagTargetForExhibitor.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.saveTagTargetByResourceType(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForExhibitor.class), exhibitorMongoDao);
        } else if (TagTargetForExhibitorProduct.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.saveTagTargetByResourceType(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForExhibitorProduct.class), exhibitorProductMongoDao);
        } else if (TagTargetForVisitor.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.saveTagTargetByResourceType(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForVisitor.class), visitorMongoDao);
        } else {
            return R.fail(ExceptionCode.NOT_FOUND);
        }
        return R.successDef();
    }

    /**
     * 获取主体标签
     *
     * @param tagTargetBusinessDTO 主体资源信息
     * @return 是否成功
     */
    @ApiOperation("获取主体标签")
    @PostMapping({"/getTargetTagList"})
    public R<List<Object>> getTargetTagList(@RequestBody TagTargetBusinessDTO tagTargetBusinessDTO) {
        if (TagTargetForExhibitor.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.getTargetTagList(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForExhibitor.class), exhibitorMongoDao);
        } else if (TagTargetForExhibitorProduct.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.getTargetTagList(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForExhibitorProduct.class), exhibitorProductMongoDao);
        } else if (TagTargetForVisitor.class.equals(tagTargetBusinessDTO.getClazz())) {
            baseTagTargetService.getTargetTagList(BeanUtil.toBean(tagTargetBusinessDTO.getT(),
                    TagTargetForVisitor.class), visitorMongoDao);
        } else {
            return R.fail(ExceptionCode.NOT_FOUND);
        }
        return R.successDef();
    }

}
